﻿using DemoUser.Core.User;
using Microsoft.AspNetCore.Mvc.Filters;
using StackExchange.Profiling.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DemoUser.Application.User.Filter
{
    public class WangActionFilter : IAsyncActionFilter
    {
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            //============== 这里是执行方法之后获取数据 ====================
            //获取返回的结果
            var actionContext = await next();
            //Console.WriteLine(actionContext);
            var returnResult = actionContext.Result;
            //Console.WriteLine(returnResult);

            if (returnResult != null)
            {
                //Console.WriteLine(returnResult.GetType().Name);
                //判断返回值类型是否是ObjectResult类型
                if (returnResult is ObjectResult objectResult)
                {
                    //获取ObjectResult对象的Value对象的类型
                    Type type = objectResult.Value.GetType();
                    //Console.WriteLine(objectResult.Value);
                    //判断ObjectResult对象的Value对象是List<自定义类型> 还是自定义类型
                    if (type.IsGenericType)
                    {

                        dynamic obList = objectResult.Value;
                        foreach (dynamic item in obList)
                        {
                            
                            //修改数据
                            UpdateData(item);
                        }
                        //Console.WriteLine("列表数据");
                    }else
                    {
                        //修改数据
                        UpdateData(objectResult.Value);
                        //Console.WriteLine("hhhh");
                    }
                }
            }
        }

        /*
         * 修改数据
         */
        private static void UpdateData(Object item)
        {
            Type oi = item.GetType();
            Random random = new Random();
            var properties = oi.GetProperties();
            for (int i = 0; i < properties.Length; i++)
            {
                if(properties[i].PropertyType.IsGenericType)
                {
                    dynamic p = properties[i].GetValue(item);
                    foreach (dynamic pro in p)
                    {
                        UpdateData(pro);
                    }

                }
                else
                {
                    if (!(properties[i].Name == "Id"))
                    {
                        if(!(properties[i].GetCustomAttributes((typeof(FilterIgnoreAttribute)), false).Length > 0))
                        {
                            if (properties[i].PropertyType == typeof(int))
                            {
                                int value = (int)properties[i].GetValue(item);
                                //随机数百分比浮动
                                properties[i].SetValue(item, (int)(value * random.NextDouble()));
                            }
                            else if (properties[i].PropertyType == typeof(double))
                            {
                                //浮点型数据变化
                                double value = (double)properties[i].GetValue(item);
                                properties[i].SetValue(item, value * random.NextDouble());
                                //浮点型数据变化
                            }
                            else
                            {
                                //其他类型数据变化
                            }
                        }
                        
                    }
                }
            }
        }
    }
}
